package com.jnks.shardjdbc;

import org.apache.shardingsphere.api.sharding.standard.PreciseShardingAlgorithm;
import org.apache.shardingsphere.api.sharding.standard.PreciseShardingValue;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;

/**
 * 根据takeoffTime精确分表
 */
public class SubDroneTableTimePreciseAlgorithm implements PreciseShardingAlgorithm<String>{



    @Override
    public String doSharding(Collection<String> availableTargetNames, PreciseShardingValue<String> shardingValue) {
        return shardingValue.getLogicTableName() + "_" + toDate(shardingValue.getValue());
    }

    public static String toDate(String date) {

        return getStanDate(date);

    }

    public static String getStanDate(String date) {
        try {
            if (date.contains("-")) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                Date k = sdf.parse(date);
                sdf = new SimpleDateFormat("yyyyMM");
                String cc = sdf.format(k);
                date = cc;
            }

        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

}
